package com.example.shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.shop.pojo.Order;
import org.apache.ibatis.annotations.*;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {

    @Update("update `order` set ${dbField} = #{value} where order_id = ${orderId}")
    int updateFieldById(@Param("dbField") String dbField,@Param("value")Object value ,@Param("orderId") Long orderId);


    @Update("<script>" +
            "update `order` set " +
            "<if test = 'et.transactionId != null'>" +
                "transaction_id = #{et.transactionId}," +
            "</if>" +
            "<if test = 'et.deliveryTime != null'>" +
                "delivery_time = #{et.deliveryTime}," +
            "</if>" +
            "<if test = 'et.orderState != null'>" +
                "order_state = #{et.orderState}," +
            "</if>" +
            "order_id = ${et.orderId} " +
            "where order_id = ${et.orderId}" +
            "</script>")
    int updateNotNull(@Param(Constants.ENTITY)Order info);

    @Insert("insert into `order`(" +
            "order_id,user_id,goods_id,courier_id,transaction_id," +
            "payment_method,order_time,delivery_time,order_state," +
            "user_purchased_goods_class,user_purchased_number," +
            "user_buy_time,real_pay) " +
            "select " +
            "${info.orderId},${info.userId},${info.goodsId},${info.courierId},#{info.transactionId}," +
            "${info.paymentMethod},#{info.orderTime},#{info.deliveryTime},${info.orderState}," +
            "#{info.userPurchasedGoodsClass},${info.userPurchasedNumber}," +
            "(select count(order_id)+1 from `order` where user_id = ${info.userId}),${info.realPay}"
    )
    int insert(@Param("info") Order order);
}
